unit StockCodePool;

interface

uses Contnrs, SyncObjs, StockDataPO, ComCtrls;

type
  TStockCodePool = class
  private
    //StockDataDao: TStockDataDAO;
    StockInfos: TObjectList;
    ProgressBar: TProgressBar;
    SumCount: Integer;
  public
    procedure setProgressBar(AProgressBar: TProgressBar);
    function GetNextStockInfo():TStockInfo;
    constructor Create(AllStockInfos: TObjectList); overload;
    destructor Destroy(); override;
  end;

var
  CodePoolCS: TCriticalSection;

implementation

{ TStockCodePool }

constructor TStockCodePool.Create(AllStockInfos: TObjectList);
begin
  CodePoolCS := TCriticalSection.Create;
  //StockDataDao := ASockDataDao;
  StockInfos := AllStockInfos;//StockDataDao.GetAllStockInfo;
  SumCount := StockInfos.Count;
end;

destructor TStockCodePool.Destroy;
begin
  StockInfos.Free;
  CodePoolCS.Free;
  inherited;
end;

function TStockCodePool.GetNextStockInfo: TStockInfo;
begin
  CodePoolCS.Enter;
  try
    if StockInfos.Count > 0 then
    begin
      Randomize;
      Result := StockInfos.Items[Random(StockInfos.Count - 1)] as TStockInfo;
      //Result := StockInfos.First as TStockInfo;

      StockInfos.Remove(Result);
      if Assigned(ProgressBar) then
        ProgressBar.Position :=  SumCount - StockInfos.Count;
    end
    else
      Result := nil;
  finally
    CodePoolCS.Leave;
  end;
end;

procedure TStockCodePool.setProgressBar(AProgressBar: TProgressBar);
begin
  ProgressBar := AProgressBar;
  ProgressBar.Min := 0;
  ProgressBar.Max := SumCount - 1;
  ProgressBar.Position := 0;
end;

end.
